package panel;

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import javax.swing.border.BevelBorder;
/**
 * Ez az osztaly egy dobozt reprezental. 
 * @author Gajdos Ferenc
 */
public class GlyphPanel extends JPanel implements Serializable {

	private static final long serialVersionUID = -1682133885093758188L;

	private JLabel jLabel = null;

	private JPanel jPanel = null;

	private JPanel buttonPanel1 = null;

	private JButton inButton1 = null;

	private JButton inButton2 = null;

	private JButton inButton3 = null;

	private JButton optionsButton = null;

	private JButton outButton1 = null;

	private JButton outButton2 = null;

	private JButton outButton3 = null;

	private transient DragProcessor dragProcessor = new DragProcessor(); 

	private JPanel jPanel1 = null;

	private boolean readyToRun = false;

	private String command;

	private JDialog dialog = null;

	public int connectedinputs = 0, connectedOutputs = 0;

	public int inputs;

	public boolean optionsSet;

	public int outputs;

	// macro generalashoz
	public String[] outputvars, inputvars;

	public ArrayList<String>[] outputVarNames;

	public boolean[] inputReady;

	public boolean commandAdded = false;

	/**
	 * @return the optionsSet
	 */
	public boolean isOptionsSet() {
		return optionsSet;
	}

	/**
	 * @param optionsSet
	 *            the optionsSet to set
	 */
	public void setOptionsSet(boolean optionsSet) {
		this.optionsSet = optionsSet;
		if (inputs == connectedinputs)
			setReadyToRun(true);
	}

	/**
	 * @return the dialog
	 */
	public JDialog getDialog() {
		return dialog;
	}

	/**
	 * @param dialog
	 *            Ezzel a fuggvennyel elmenthetjuk a dialogus 
	 *            ablkot amivel beallitottuk a parameterket
	 */
	public void setDialog(JDialog dialog) {
		this.dialog = dialog;
	}

	/**
	 * a makro generalaskor ezt a fuggvenyt hivjuk meg a makro kod kiolvasasahoz.
	 * @return the command
	 */
	public String getCommand() {
		String result = "";
		String[] comm = new String[outputVarNames.length];
		if (jLabel.getText().contentEquals("Sample Images") && command != null
				&& outputVarNames.length > 0) {
			comm[0] = String.format("%s%n", command);
		}
		if (jLabel.getText().contentEquals("Display Image")
				&& inputvars[0] != null) {
			if (command != null && !command.isEmpty())
				result = String.format(
						"selectWindow(%s);%nrename(\"%s\");%n", inputvars[0],
						command);
			else
				result = String.format("selectWindow(%s);%n", inputvars[0]);
		} else if (jLabel.getText().contentEquals("Histogram")
				&& inputvars[0] != null) {
			result = String
					.format(
							"selectWindow(%s);%nrun(\"Histogram\");%nselectWindow(%s);%nclose();%n",
							inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Threshold")
				&& inputvars[0] != null && outputVarNames.length > 0) {
			comm[0] = String.format("selectWindow(%s)%n%s%n", inputvars[0],
					command);
		} else if (jLabel.getText().contentEquals("Add")
				&& inputvars[0] != null && inputvars[1] != null
				&& outputVarNames.length > 0) {
			comm[0] = String
					.format(
							"imageCalculator(\"Add stack\", %s,%s);%n selectWindow(%s);%nclose();%nselectWindow(%s);%n",
							inputvars[0], inputvars[1], inputvars[1],inputvars[0]);
		} else if (jLabel.getText().contentEquals("Subtract")
				&& inputvars[0] != null && inputvars[1] != null
				&& outputVarNames.length > 0) {
			comm[0] = String
					.format(
							"imageCalculator(\"Subtract stack\", %s,%s);%n selectWindow(%s);%nclose();%nselectWindow(%s);%n",
							inputvars[0], inputvars[1], inputvars[1],inputvars[0]);
		} else if (jLabel.getText().contentEquals("Multiply")
				&& inputvars[0] != null && inputvars[1] != null
				&& outputVarNames.length > 0) {
			 comm[0] = String
			 .format(
			 "imageCalculator(\"Multiply stack\", %s,%s);%n selectWindow(%s);%nclose();%nselectWindow(%s);%n",
			 inputvars[0], inputvars[1], inputvars[1],inputvars[0]);			
		} else if (jLabel.getText().contentEquals("Divide")
				&& inputvars[0] != null && inputvars[1] != null
				&& outputVarNames.length > 0) {
			comm[0] = String
					.format(
							"imageCalculator(\"Divide stack\", %s,%s);%n selectWindow(%s);%nclose()%nselectWindow(%s)%n",
							inputvars[0], inputvars[1], inputvars[1],inputvars[0]);
		} else if (jLabel.getText().contentEquals("Split")
				&& inputvars[0] != null && outputVarNames.length == 3) {

			result = String.format(
					"selectWindow(%s);%nrun(\"Split Channels\");%n",
					inputvars[0]);
			comm[0] = String.format("selectWindow(%s+\" (red)\");%nrename(%s+\"(red)\");%n",
					inputvars[0],inputvars[0]);
			comm[1] = String.format("selectWindow(%s+\" (green)\");%nrename(%s+\"(green)\");%n",
					inputvars[0],inputvars[0]);
			comm[2] = String.format("selectWindow(%s+\" (blue)\");%nrename(%s+\"(blue)\");%n",
					inputvars[0],inputvars[0]);
			
			
		} else if (jLabel.getText().contentEquals("Merge Channels")
				&& inputvars[0] != null && inputvars[1] != null
				&& inputvars[2] != null) {
			comm[0] = String.format(
					"run(\"Merge Channels...\", \"red=[\"+%s+\"] green=[\"+%s+\"] blue=[\"+%s+\"]"
							+ "gray=*None* create\");", inputvars[0],
					inputvars[1], inputvars[2]);
		} else if (jLabel.getText().contentEquals("User Defined")
				|| jLabel.getText().contentEquals("Ideal Filter")
				|| jLabel.getText().contentEquals("Butterworth Filter")
				&& outputVarNames.length > 0) {
			comm[0] = String.format("%s%n", command);
		} else if (jLabel.getText().contentEquals("Surface plot")
				&& inputvars[0] != null) {
			result = String.format(
					"%nselectWindow(%s);%n%s%nselectWindow(%s);%nclose();",
					inputvars[0], command, inputvars[0]);
		} else if (jLabel.getText().contentEquals("Constant")
				&& outputVarNames.length > 0) {
			comm[0] = String.format("%s%n", command);

		} else if (jLabel.getText().contentEquals("Canvas Resize")
				&& inputvars[0] != null && outputVarNames.length > 0) {
			comm[0] = String.format("%nselectWindow(%s);%n%s%n", inputvars[0],
					command);
		} else if (jLabel.getText().contentEquals("Flip Horizontally")
				&& inputvars[0] != null && outputVarNames.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Flip Horizontally\");",
					inputvars[0]);
		} else if (jLabel.getText().contentEquals("Flip Vertically")
				&& inputvars[0] != null && outputVarNames.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Flip Vertically\");",
					inputvars[0]);
		} else if (jLabel.getText().contentEquals("Translate")
				&& inputvars[0] != null && outputVarNames.length > 0) {
			comm[0] = String.format("selectWindow(%s);%n%s", inputvars[0],
					command);
		} else if (jLabel.getText().contentEquals("Rotate 90 Degrees Right")
				&& inputvars[0] != null && outputVarNames.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Rotate 90 Degrees Right\");",
					inputvars[0]);
		} else if (jLabel.getText().contentEquals("Rotate 90 Degrees Right")
				&& inputvars[0] != null && outputVarNames.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Rotate 90 Degrees Left\");",
					inputvars[0]);
		} else if ((jLabel.getText().contentEquals("Rotate")
				|| jLabel.getText().contentEquals("Crop")
				|| jLabel.getText().contentEquals("Enhance Contrast")
				|| jLabel.getText().contentEquals("Unsharp Mask")
				|| jLabel.getText().contentEquals("Add Specified Noise") 
				|| jLabel.getText().contentEquals("Clip")
				|| jLabel.getText().contentEquals("Scale"))
				&& inputvars[0] != null && outputVarNames.length > 0) {
			comm[0] = String.format("selectWindow(%s);%n%s", inputvars[0],
					command);
		} else if (jLabel.getText().contentEquals("Inset")
				&& inputvars[0] != null && inputvars[1] != null
				&& outputvars.length > 0) {
			comm[0] = String
					.format(
							"%s%nselectWindow(%s);%nwidth=getWidth();%nheight=getHeight();%nrun(\"Copy\");%n",
							command, inputvars[0]);
			comm[0] = String
					.format(
							"%sselectWindow(%s);%nsetPasteMode(\"Copy\");%nmakeRectangle(insertAtX,insertAtY, width,height);%nrun(\"Paste\");makeRectangle(0,0, 0,0);%nselectWindow(%s);%nclose();%n",
							comm[0], inputvars[1],inputvars[0]);
		} else if (jLabel.getText().contentEquals("FFT")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String
					.format(
							"selectWindow(%s);%nrun(\"FFT Options...\", \"  complex do\");%nselectWindow(%s);%nclose();%nrename(%s);%n",
							inputvars[0], inputvars[0], inputvars[0]);
			
		} else if (jLabel.getText().contentEquals("Inverse FFT")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String
					.format(
							"selectWindow(%s);%nrun(\"Inverse FFT\");%nselectWindow(%s);%nclose();%n",
							inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("8-bit")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"8-bit\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("16-bit")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"16-bit\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("32-bit")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"32-bit\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("8-bit Color")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"8-bit Color\", \"number=%s\");%n", inputvars[0],
					command);
		} else if (jLabel.getText().contentEquals("RGB")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"RGB Color\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Log")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"Log\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Exp")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"Exp\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Square")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"Square\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Square Root")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Square Root\");%n", inputvars[0],
					inputvars[0]);
		} else if (jLabel.getText().contentEquals("Reciprocal")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Reciprocal\");%n", inputvars[0],
					inputvars[0]);
		} else if (jLabel.getText().contentEquals("NaN Background")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"NaN Background\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Abs")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"Abs\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Add Constant")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Add...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Subtract Constant")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String
					.format(
							"selectWindow(%s);%nrun(\"Subtract...\", \"value=%s\");;%n",
							inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Multiply Constant")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String
					.format(
							"selectWindow(%s);%nrun(\"Multiply...\", \"value=%s\");;%n",
							inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Divide Constant")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Divide...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("AND")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"AND...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("OR") && inputvars[0] != null
				&& outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"OR...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("XOR")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"XOR...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Min")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Min...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Max")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Max...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Gamma")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Gamma...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Set")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Set...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Macro")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Macro...\", \"value=%s\");;%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Convolve")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%n%s%n", inputvars[0],
					command);
		} else if (jLabel.getText().contentEquals("Gaussian Blur")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String
					.format(
							"selectWindow(%s);%nrun(\"Gaussian Blur...\", \"sigma=%s\");%n",
							inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Mean")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Mean...\", \"radius=%s\");%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Minimum")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Minimum...\", \"radius=%s\");%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Maximum")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Maximum...\", \"radius=%s\");%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Median")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Median...\", \"radius=%s\");%n",
					inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Variance")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String
					.format(
							"selectWindow(%s);%nrun(\"Variance...\", \"radius=%s\");%n",
							inputvars[0], command);
		} else if (jLabel.getText().contentEquals("Salt and Pepper")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format(
					"selectWindow(%s);%nrun(\"Salt and Pepper\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Add Noise")
				&& inputvars[0] != null && outputvars.length > 0) {
			comm[0] = String.format("selectWindow(%s);%nrun(\"Add Noise\");%n",
					inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("First Stack Slice")&&inputvars[0]!=null && outputvars.length>0) {
			comm[0]=String.format("selectWindow(%s);%nStack.setSlice(1);%nrun(\"Duplicate...\", \"title=\"+%s+\"real\");%nselectWindow(%s)%nclose();", inputvars[0],inputvars[0], inputvars[0]);
		} else if (jLabel.getText().contentEquals("Second Stack Slice")&&inputvars[0]!=null && outputvars.length>0) {
			comm[0]=String.format("selectWindow(%s);%nStack.setSlice(2);%nrun(\"Duplicate...\", \"title=\"+%s+\"imag\");%nselectWindow(%s)%nclose();", inputvars[0],inputvars[0], inputvars[0]);
		}
		
		//ha a kimenet tobb dobozra is ra van kotve
		if (outputVarNames.length > 0)
			for (int i = 0; i < outputVarNames.length; i++) {
				
				if (outputVarNames[i]!=null && !outputVarNames[i].isEmpty()) {
					ArrayList<String> arrayList = outputVarNames[i];
					int numOfDuplicate = arrayList.size() - 1;
					if (numOfDuplicate > 0) {
						comm[i] = String.format("%s%n%s=getTitle();", comm[i],
								arrayList.get(0));
						for (int j = 1; j < arrayList.size(); j++) {
							comm[i] = String
									.format(
											"%s%nn=getSliceNumber();%nrun(\"Duplicate...\",\"x duplicate range=1-n\");%n%s=getTitle();",
											comm[i], arrayList.get(j));
						}
					} else {
						if (arrayList.size() > 0)
							comm[i] = String.format("%s%n%s=getTitle();",
									comm[i], arrayList.get(0));

					}
				}

			}

		for (int i = 0; i < comm.length; i++) {
			result = String.format("%s%n%s", result, comm[i]);
		}
		for (int i = 0; i < outputVarNames.length; i++) {
			outputVarNames[i].clear();
		}
		commandAdded = true;
		return result;
	}

	/**
	 * @param command
	 *            the command to set
	 */
	public void setCommand(String command) {
		this.command = command;
	}

	/**
	 * @return the readyToRun
	 */
	public boolean isReadyToRun() {
		return readyToRun;
	}

	
	public String getGlyphName() {
		return jLabel.getText();

	}

	/**
	 * @param readyToRun
	 *            the readyToRun to set
	 */
	public void setReadyToRun(boolean readyToRun) {
		this.readyToRun = readyToRun;
		if (readyToRun)
			jPanel1.setBackground(Color.green);
		else
			jPanel1.setBackground(Color.red);
		this.doLayout();
	}
	/**
	 * kijelolte teszi a dobozt
	 */
	public void setActive() {
		buttonPanel1.setBackground(Color.gray);
	}
	/**
	 * megszunteti a kijelolest
	 */
	public void setNormal() {
		buttonPanel1.setBackground(Color.DARK_GRAY);
	}

	/**
	 * This is the default constructor
	 */
	public GlyphPanel() {
		super();
		initialize();
	}
	/**
	 * Letrehoz a parameterkenek megelelo dobozt.
	 * @param inputs
	 * @param outputs
	 * @param option
	 * @param name
	 */
	@SuppressWarnings("unchecked")
	public GlyphPanel(int inputs, int outputs, Boolean option, String name) {
		super();
		initialize();
		jLabel.setText(name);
		this.inputs = inputs;
		this.outputs = outputs;
		inputvars = new String[inputs];
		outputvars = new String[outputs];
		inputReady = new boolean[inputs];
		java.util.Arrays.fill(inputReady, false);
		outputVarNames = new ArrayList[outputs];
		for (int i = 0; i < outputVarNames.length; i++) {
			outputVarNames[i] = new ArrayList<String>();
		}
		switch (inputs) {
		case 0:
			inButton1.setVisible(false);
			inButton2.setVisible(false);
			inButton3.setVisible(false);
		case 1:
			inButton2.setVisible(false);
			inButton3.setVisible(false);
			break;
		case 2:
			inButton3.setVisible(false);
			break;
		default:
			break;
		}
		switch (outputs) {
		case 0:
			outButton1.setVisible(false);
			outButton2.setVisible(false);
			outButton3.setVisible(false);
		case 1:
			outButton2.setVisible(false);
			outButton3.setVisible(false);
			break;
		case 2:
			outButton3.setVisible(false);
			break;

		default:
			break;
		}
		if (!option) {
			optionsButton.setVisible(false);
			setOptionsSet(true);
		}

		if (name.contentEquals("Display Image")) {
			this.setOptionsSet(true);

		}

	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel = new JLabel();
		jLabel.setForeground(Color.orange);
		jLabel.setText("JLabel");
		jLabel.setHorizontalAlignment(SwingConstants.CENTER);
		jLabel.setVerticalAlignment(SwingConstants.TOP);
		jLabel.setVerticalTextPosition(SwingConstants.TOP);
		this.setFocusable(false);
		this.setSize(79, 46);
		this.setOpaque(false);
		this.setLayout(new BorderLayout());
		this.add(jLabel, BorderLayout.SOUTH);
		this.add(getJPanel(), BorderLayout.CENTER);
		this.addMouseListener(dragProcessor);
		this.addMouseMotionListener(dragProcessor);
		this.setFocusable(false);
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setOpaque(false);
			jPanel.setLayout(null);
			jPanel.add(getButtonPanel1(), null);
		}
		return jPanel;
	}
	
	protected class DragProcessor extends MouseAdapter implements
			MouseListener, MouseMotionListener, Serializable {
		protected Point pressPoint, releasePoint;
        private static final long serialVersionUID = 1L;
		public void mouseDragged(MouseEvent e) {
			releasePoint = e.getPoint();
			int xDiff = pressPoint.x - releasePoint.x;
			int yDiff = pressPoint.y - releasePoint.y;

			Rectangle b = e.getComponent().getBounds();
			Point p = b.getLocation();
			SwingUtilities
					.convertPointToScreen(p, e.getComponent().getParent());
			p.x -= xDiff;
			p.y -= yDiff;

			SwingUtilities.convertPointFromScreen(p, GlyphPanel.this
					.getParent());
			if (p.x <= 0) {
				p.x = 1;
			}
			if (p.x > GlyphPanel.this.getParent().getWidth() - b.width) {
				p.x = GlyphPanel.this.getParent().getWidth() - b.width;
			}
			if (p.y <= 0) {
				p.y = 1;
			}
			if (p.y > GlyphPanel.this.getParent().getHeight() - b.height) {
				p.y = GlyphPanel.this.getParent().getHeight() - b.height;
			}
			setLocation(p);			
			getParent().repaint();
			
		}

		public void mouseMoved(MouseEvent e) {
		}

		public void mousePressed(MouseEvent e) {			
			pressPoint = e.getPoint();		
		}

		public void mouseReleased(MouseEvent e) {
			releasePoint = e.getPoint();
			int xDiff = pressPoint.x - releasePoint.x;
			int yDiff = pressPoint.y - releasePoint.y;

			Rectangle b = e.getComponent().getBounds();
			Point p = b.getLocation();
			SwingUtilities
					.convertPointToScreen(p, e.getComponent().getParent());
			p.x -= xDiff;
			p.y -= yDiff;

			SwingUtilities.convertPointFromScreen(p, GlyphPanel.this
					.getParent());
			if (p.x <= 0) {
				p.x = 1;
			}
			if (p.x > GlyphPanel.this.getParent().getWidth() - b.width) {
				p.x = GlyphPanel.this.getParent().getWidth() - b.width;
			}
			if (p.y <= 0) {
				p.y = 1;
			}
			if (p.y > GlyphPanel.this.getParent().getHeight() - b.height) {
				p.y = GlyphPanel.this.getParent().getHeight() - b.height;
			}
			setLocation(p);
			getParent().repaint();

		}
	}

	/**
	 * This method initializes buttonPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel1() {
		if (buttonPanel1 == null) {
			buttonPanel1 = new JPanel();
			buttonPanel1.setBackground(Color.DARK_GRAY);
			buttonPanel1.setLayout(null);
			buttonPanel1.setBounds(new Rectangle(30, 0, 55, 29));
			buttonPanel1.setBorder(BorderFactory
					.createEtchedBorder(EtchedBorder.RAISED));
			buttonPanel1.add(getInButton1(), null);
			buttonPanel1.add(getInButton2(), null);
			buttonPanel1.add(getInButton3(), null);
			buttonPanel1.add(getOptionsButton(), null);
			buttonPanel1.add(getOutButton1(), null);
			buttonPanel1.add(getOutButton2(), null);
			buttonPanel1.add(getOutButton3(), null);
			buttonPanel1.add(getJPanel1(), null);
		}
		return buttonPanel1;
	}

	/**
	 * This method initializes inButton1
	 * 
	 * @return javax.swing.JButton
	 */
	public JButton getInButton1() {
		if (inButton1 == null) {
			inButton1 = new JButton();
			inButton1.setBounds(new Rectangle(0, 3, 6, 6));
			inButton1.setName("i1");
			inButton1.setFocusable(false);

		}
		return inButton1;
	}

	/**
	 * This method initializes inButton2
	 * 
	 * @return javax.swing.JButton
	 */
	public JButton getInButton2() {
		if (inButton2 == null) {
			inButton2 = new JButton();
			inButton2.setBounds(new Rectangle(0, 12, 6, 6));
			inButton2.setName("i2");
			inButton2.setFocusable(false);
		}
		return inButton2;
	}

	/**
	 * This method initializes inButton3
	 * 
	 * @return javax.swing.JButton
	 */
	public JButton getInButton3() {
		if (inButton3 == null) {
			inButton3 = new JButton();
			inButton3.setBounds(new Rectangle(0, 21, 6, 6));
			inButton3.setName("i3");
			inButton3.setFocusable(false);
		}
		return inButton3;
	}

	/**
	 * This method initializes optionsButton
	 * 
	 * @return javax.swing.JButton
	 */
	public JButton getOptionsButton() {
		if (optionsButton == null) {
			optionsButton = new JButton();
			optionsButton.setFocusable(false);
			optionsButton.setBounds(new Rectangle(22, 10, 10, 10));
			optionsButton.setName("setup");
		}
		return optionsButton;
	}

	/**
	 * This method initializes outButton1
	 * 
	 * @return javax.swing.JButton
	 */
	public JButton getOutButton1() {
		if (outButton1 == null) {
			outButton1 = new JButton();
			outButton1.setBounds(new Rectangle(49, 3, 6, 6));
			outButton1.setName("o1");
			outButton1.setFocusable(false);
		}
		return outButton1;
	}

	/**
	 * This method initializes outButton2
	 * 
	 * @return javax.swing.JButton
	 */
	public JButton getOutButton2() {
		if (outButton2 == null) {
			outButton2 = new JButton();
			outButton2.setBounds(new Rectangle(49, 12, 6, 6));
			outButton2.setName("o2");
			outButton2.setFocusable(false);
		}
		return outButton2;
	}

	/**
	 * This method initializes outButton3
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOutButton3() {
		if (outButton3 == null) {
			outButton3 = new JButton();
			outButton3.setBounds(new Rectangle(49, 21, 6, 6));
			outButton3.setName("o3");
			outButton3.setFocusable(false);
		}
		return outButton3;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.setBounds(new Rectangle(22, 0, 10, 10));
			jPanel1.setBorder(BorderFactory
					.createBevelBorder(BevelBorder.LOWERED));
			jPanel1.setBackground(Color.red);
		}
		return jPanel1;
	}

	public void addLinePainter(ActionListener listener) {
		if (inButton1 != null)
			inButton1.addActionListener(listener);
		if (inButton2 != null)
			inButton2.addActionListener(listener);
		if (inButton3 != null)
			inButton3.addActionListener(listener);
		if (outButton1 != null)
			outButton1.addActionListener(listener);
		if (outButton2 != null)
			outButton2.addActionListener(listener);
		if (outButton3 != null)
			outButton3.addActionListener(listener);

	}

	public void addOptionsListener(ActionListener listener) {
		if (optionsButton != null)
			optionsButton.addActionListener(listener);
	}

	public boolean isAllInputReady() {
		if (inputReady.length == 0)
			return true;
		else {
			for (int i = 0; i < inputReady.length; i++)
				if (!inputReady[i])
					return false;
			return true;
		}
	}
} 
